package com.zhongwang.business.module.baseCostManage.controller;
import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.module.baseCostManage.model.InputCostsModel;
import com.zhongwang.business.module.baseCostManage.service.InputCostsService;
import com.zhongwang.business.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 *  控制层
 * @author China.kxx
 * @date 2020/09/18 20:10
 */
@Slf4j
@Controller
@RequestMapping("/dataEntry/cutting")
public class InputCostsController extends BaseController {


    //页面文件路径
    private String prefix = "/dataEntry";
    @Autowired
    private InputCostsService inputCostsService;
    //加载页面
    @GetMapping("/manpower")
    public String manpower(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/manpower";
    }
    /**
     * 加载工时列表数据
     * @param inputCostsModel
     * @return
     */
    @ResponseBody
    @PostMapping("/manpower")
    public JsonResult manpower(InputCostsModel inputCostsModel) {
        List<InputCostsModel> list = inputCostsService.inputCuttingTime(inputCostsModel);
        return jsonResult(list);
    }
    /**
     * 加载辅材列表数据
     * @param inputCostsModel
     * @return
     */
    @ResponseBody
    @PostMapping("/material")
    public JsonResult material(InputCostsModel inputCostsModel) {
        List<InputCostsModel> list = inputCostsService.inputCuttingMaterial(inputCostsModel);
        return jsonResult(list);
    }
    /**
     * 工时编辑模态框数据回显
     * @param manId
     * @return
     */
    @ResponseBody
    @PostMapping("/oneManpowerTime")
    public InputCostsModel selectOneManpowerTime(Integer manId) {
        return inputCostsService.selectOneManpowerTime(manId);
    }
    /**
     * 辅材编辑模态框数据回显
     * @param mid
     * @return
     */
    @ResponseBody
    @PostMapping("/oneMaterial")
    public InputCostsModel selectOneMaterial(Integer mid) {
        return inputCostsService.selectOneMaterial(mid);
    }


    /**
     * 保存添加人工数据
     * @param manpower
     * @return
     */
//    @ResponseBody
//    @PostMapping("/manpowerSave")
//    public JsonResult saveMan(InputCostsModel manpower){
//        JsonResult jsonResult = new JsonResult();
//        int flg=inputCostsService.saveManpowerInfo(manpower);
//        if(flg>0) {
//            jsonResult.setType(JsonResult.ResultType.SUCCESS);
//            jsonResult.setData(flg);
//        }else{
//            jsonResult.setType(JsonResult.ResultType.ERROR);
//        }
//        return jsonResult;
//    }
    /**
     *保存添加人工数据，验证当数据被接收无法编辑
     * @param manpower
     * @return
     */
    @ResponseBody
    @PostMapping("/manpowerSave")
    public JsonResult saveMan(InputCostsModel manpower){

        int flg=inputCostsService.selectVerify(manpower);
        if (flg == 1) {
            return error("数据已被接收，无法进行编辑！！需联系商务科");
        } else {
            return jsonResult(inputCostsService.saveManpowerInfo(manpower));
        }
    }

    /**
     * 工时数据删除(单条)
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/delManHour")
    public JsonResult delManHour(Integer manId) {
        int row = inputCostsService.delManHour(manId);
        if (row == 1) {
            return error("数据已被接收，无法进行删除！！需联系商务科");
        } else {
            return jsonResult(inputCostsService.deleteManHour(manId));
        }
    }

    /**
     * 辅材数据删除(单条)
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/delMaterial")
    public JsonResult delMaterial(Integer mid) {
        int row = inputCostsService.delMaterial(mid);
        if (row ==1) {
            return error("数据已被接收，无法进行删除！！需联系商务科");
        } else {
            return jsonResult(inputCostsService.deleteMaterial(mid)) ;
        }

    }
    /**
     * 工时数据删除(全部清空)
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/delManHourAll")
    public JsonResult delManHourAll(InputCostsModel inputCostsModel) {
        int row = inputCostsService.delManHourAll(inputCostsModel);
        if (row == -1) {
            return error("数据已被接收，无法进行删除！！需联系商务科");
        } else if(row == 0){
            return error("无数据，操作失败");
        } else if(row == 1){
            return jsonResult(inputCostsService.deleteManHourAll(inputCostsModel));
        } else {
            return JsonResult.fail();
        }
    }

    /**
     * 辅材数据删除(全部清空)
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/delMatAll")
    public JsonResult delMatAll(InputCostsModel inputCostsModel) {
        int row = inputCostsService.delMatAll(inputCostsModel);
        if (row == -1) {
            return error("数据已被接收，无法进行删除！！需联系商务科");
        } else if(row == 0){
            return error("无数据，操作失败");
        } else if(row == 1){
            return jsonResult(inputCostsService.deleteMatAll(inputCostsModel)) ;
        } else {
            return JsonResult.fail();
        }

    }
}